package com.smart.bookshelf

import android.content.pm.PackageManager
import android.os.Build
import android.util.Log
import android.widget.Toast
import com.proembed.service.MyService
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugins.GeneratedPluginRegistrant


class MainActivity: FlutterActivity() {

    override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
//        super.configureFlutterEngine(flutterEngine)
        GeneratedPluginRegistrant.registerWith(flutterEngine)
        val android = MethodChannel(flutterEngine.dartExecutor.binaryMessenger,"toAndroid")

        android.setMethodCallHandler { call, result ->
            run {
                if (call.method != null) {
                    if ("hideBar" == call.method) {
                        result.success(hideBar())
                    }
                    if ("showBar" == call.method) {
                        result.success(showBar())
                    }
                    if ("jump" == call.method) {
                        result.success(jump())
                    }

                    if ("getAppVersionCode" == call.method) {
                        result.success(getAppVersionCode())
                    }

                    if ("install" == call.method) {
                        result.success(install(call.argument("message")))
                    }

                } else {
                    result.notImplemented()
                }
            }
        }

    }

    private fun hideBar():String{
        val myService = MyService(this)
        myService.hideNavBar = true
        myService.setSwitch("StatusBar",false)
//        lockScreen()
        return "success"
    }

    private fun showBar():String{
        val myService = MyService(this)
        myService.hideNavBar = false
        myService.setSwitch("StatusBar",true)
        return "success"
    }

    private fun jump():String{
        val packageName = "cmj.mode.bookreader"

        val packageManager: PackageManager = packageManager
//        val intent = Intent("android.intent.action.MAIN");
        val intent = packageManager.getLaunchIntentForPackage(packageName)
        if (intent == null) {
            Toast.makeText(this@MainActivity, "未安装", Toast.LENGTH_LONG).show()
        } else {
            startActivity(intent)
        }
//        val intent2 = Intent("android.intent.action.MAIN")
//        startActivity(intent2)

        return "success"
    }

    private fun install(path:String?):String{
        val myService = MyService(this)
        myService.silentInstallApk(path,"com.smart.bookshelf",true)
        return "success"
    }

    private fun getAppVersionCode():Int{
        try {
            val packageInfo = packageManager.getPackageInfo(packageName, 0)
            val versionName = packageInfo.versionName // 版本名称
            // 使用版本信息
            return (if (Build.VERSION.SDK_INT > 28) packageInfo.longVersionCode else packageInfo.versionCode).toInt()
        } catch (e: PackageManager.NameNotFoundException) {
            e.printStackTrace()
        }
        return 0
    }

}
